package com.lyh.commom.BizException;

import com.mongodb.client.*;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.Arrays;
import java.util.List;

public class MogoTest {

    public static void main(String[] args) {

// 创建 MongoDB 客户端
        MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");

// 获取数据库和集合
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection<Document> collection = database.getCollection("test");

// 执行聚合函数
        List<Bson> pipeline = Arrays.asList(
            Aggregates.match(Filters.gte("create_time",1681307201000L)),
            Aggregates.skip(0),
            Aggregates.limit(3)

        );
        List<Bson> pipeline2 = Arrays.asList(
            Aggregates.match(Filters.gte("create_time",1681307201000L)),
            Aggregates.count()

            );


        AggregateIterable<Document> result = collection.aggregate(pipeline);
        AggregateIterable<Document> result2 = collection.aggregate(pipeline2);

        for (Document doc : result) {
            System.out.println(doc.toJson());
        }

// 输出结果
        for (Document doc : result2) {
            System.out.println(doc.toJson());
        }
    }

}
